<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-3-21
 * Time: 下午8:58
 * 地图模式控制器
 */
namespace Factory\Controller;
class MapController extends BaseController {

    //地图
    public  function  index(){
//        P($_SESSION);
        //2---厂商  3---运营商
        if(session('fac_type') == 2){
            if (session('fac_key')){
                $uid = session("fac_key");
                $map[] = array('yy_user_id'=>session('uid'));
            }else{
                $uid = session("fac_login_name");
            }
            $map[] = array('plant_type'=>$uid);
        }elseif(session('fac_type') == 3){
            $map[] = array('plant_attr'=>3);
            if(session('pid') != 0){
                $map[] = array('yy_user_id'=>session('uid'));
            }else{
                $yyuserid = M()->query("select id from ev_fac where id='".session('uid')."' OR pid='".session('uid')."'");
                foreach($yyuserid as $k=>$v){
                    $yyuserids .= $v['id'].",";
                }
                $yyuserids = trim($yyuserids,',');
                $map['yy_user_id'] = array('IN',$yyuserids);
            }
        }

        //所有站点
        $plant=M("plant")->where($map)->select();
        $this->assign("count",count($plant));
//        echo M()->getLastSql()."<br />";

        //预设站点
        $map['current_state'] = array('neq',1);
        $map['is_active'] = array('neq',1);
        $yushe=M("plant")->where($map)->select();
        $this->assign("yushe",count($yushe));
//        echo M()->getLastSql()."<br />";
        
        //实时站点
        $map['is_active'] = 1;
        $map['current_state'] = 1;
        $shishi=M("plant")->where($map)->select();
        $this->assign("shishi",count($shishi));
        //$this->coordinate = "116.490816,39.968931";
//        echo M()->getLastSql();
        $this->china = D("Admin/Region")->getChinaArea();
        $this->display();
    }

    //站点模糊查询
    public  function map_name(){
        if(!empty($_GET['name'])){
            $name = I("name");
        }
        if(session("fac_type") == 2){
            if (session('fac_key')){
                $condition_str="and yy_user_id='" . session('uid') . "' and plant_type='".session('fac_key')."'";
            }else{
                $condition_str="and plant_type='".session('fac_login_name')."'";
            }
        }elseif(session("fac_type") == 3){
            if(session('pid') != 0){
                $operation = "and plant_attr=3 and yy_user_id='".session('uid')."'";
            }else{
                $yyuserid = M()->query("select id from ev_fac where id='".session('uid')."' OR pid='".session('uid')."'");
                foreach($yyuserid as $k=>$v){
                    $yyuserids .= $v['id'].",";
                }
                $yyuserids = trim($yyuserids,',');
                $operation = "and plant_attr=3 and yy_user_id IN($yyuserids)";
            }
        }
        $plant=M()->query("select id,name,current_state,address,is_active,coordinate from ev_plant where name like '%".$name."%' {$operation}  {$condition_str}");
//        echo M()->getLastSql();die;
        $AddMarkString = '';
        for( $i=0;$i<count($plant);$i++){
            if(!empty($plant[$i]['coordinate'])){
                $arr=explode(",",$plant[$i]['coordinate']);
                $AddMarkString .="mark".$plant[$i]['id'].",".$arr[0].",".$arr[1].",".$plant[$i]['name'].",".$plant[$i]['current_state'].",".$plant[$i]['address'].",".$plant[$i][id].",".$plant[$i]['is_active'].",".$plant[$i]['current_state']."+";
            }
        }
        if($AddMarkString){
            echo $AddMarkString;
        }else{
            echo error_get_last();
        }
    }

    //省市区站点查询
    public  function map_loc(){

        if(!empty($_GET['location_f_1'])){

            if(I('location_s_1') == 110100 || I('location_s_1') == 120100 || I('location_s_1') == 310100 || I('location_s_1') == 500100){
                if(I('location_s_1') == I('location_t_1')-1){
                    $location = I('location_f_1').",".I('location_s_1');
                }else{
                    $location = I('location_f_1').",".I('location_s_1').",".I('location_t_1');
                }
            }else if(I('location_s_1') == I('location_t_1')-1){
                $location = I('location_f_1').",".I('location_s_1');
            }else{
                $location = I('location_f_1').",".I('location_s_1').",".I('location_t_1');
            }
        }

        if(session("fac_type") == 2){
            if (session('fac_key')){
                $condition_str="and yy_user_id='" . session('uid') . "' and plant_type='".session('fac_key')."'";
            }else{
                $condition_str="and plant_type='".session('fac_login_name')."'";
            }
        }elseif(session("fac_type") == 3){
            if(session('pid') != 0){
                $operation = "and plant_attr=3 and yy_user_id='".session('uid')."'";
            }else{
                $yyuserid = M()->query("select id from ev_fac where id='".session('uid')."' OR pid='".session('uid')."'");
                foreach($yyuserid as $k=>$v){
                    $yyuserids .= $v['id'].",";
                }
                $yyuserids = trim($yyuserids,',');
                $operation = "and plant_attr=3 and yy_user_id IN($yyuserids)";
            }
        }

        $plant=M()->query("select id,name,current_state,address,is_active,coordinate from ev_plant where location like '%$location%' {$operation} {$condition_str}");
//        echo M()->getLastSql();die;
        $AddMarkString = '';
        for( $i=0;$i<count($plant);$i++){
            if(!empty($plant[$i]['coordinate'])){
                $arr=explode(",",$plant[$i]['coordinate']);
                $AddMarkString .="mark".$plant[$i]['id'].",".$arr[0].",".$arr[1].",".$plant[$i]['name'].",".$plant[$i]['current_state'].",".$plant[$i]['address'].",".$plant[$i][id].",".$plant[$i]['is_active'].",".$plant[$i]['current_state']."+";
            }
        }
        if($AddMarkString){
            echo $AddMarkString;
        }else{
            echo error_get_last();
        }
    }

    //查询所有站点地图坐标
    public  function map_now(){

        if(session("fac_type") == 2){
            if (session('fac_key')){
                $condition_str="yy_user_id='" . session('uid') . "' and plant_type='".session('fac_key')."'";
            }else{
                $condition_str="plant_type='".session('fac_login_name')."'";
            }
        }elseif(session("fac_type") == 3){
            if(session('pid') != 0){
                $operation = "plant_attr=3 and yy_user_id='".session('uid')."'";
            }else{
                $yyuserid = M()->query("select id from ev_fac where id='".session('uid')."' OR pid='".session('uid')."'");
                foreach($yyuserid as $k=>$v){
                    $yyuserids .= $v['id'].",";
                }
                $yyuserids = trim($yyuserids,',');
                $operation = "plant_attr=3 and yy_user_id IN($yyuserids)";
            }
        }
        $plant=M()->query("select id,name,current_state,address,is_active,coordinate from ev_plant  where {$operation} {$condition_str}");
        $AddMarkString = '';
        for( $i=0;$i<count($plant);$i++){
            if(!empty($plant[$i]['coordinate'])){
                $arr=explode(",",$plant[$i]['coordinate']);
                $AddMarkString .="mark".$plant[$i]['id'].",".$arr[0].",".$arr[1].",".$plant[$i]['name'].",".$plant[$i]['current_state'].",".$plant[$i]['address'].",".$plant[$i][id].",".$plant[$i]['is_active'].",".$plant[$i]['current_state']."+";
            }
        }
        if($AddMarkString){
            echo $AddMarkString;
        }else{
            echo error_get_last();
        }
    }

    //预设站点
    public  function map_will(){

        if(session("fac_type") == 2){
            if (session('fac_key')){
                $condition_str="and yy_user_id='" . session('uid') . "' and plant_type='".session('fac_key')."'";
            }else{
                $condition_str="and plant_type='".session('fac_login_name')."'";
            }
        }elseif(session("fac_type") == 3){
            if(session('pid') != 0){
                $operation = "and plant_attr=3 and yy_user_id='".session('uid')."'";
            }else{
                $yyuserid = M()->query("select id from ev_fac where id='".session('uid')."' OR pid='".session('uid')."'");
                foreach($yyuserid as $k=>$v){
                    $yyuserids .= $v['id'].",";
                }
                $yyuserids = trim($yyuserids,',');
                $operation = "and plant_attr=3 and yy_user_id IN($yyuserids)";
            }
        }

        $plant=M()->query("select id,name,current_state,address,is_active,coordinate from ev_plant where current_state<>1 and is_active<>1  {$operation} {$condition_str}");
        $AddMarkString = '';
        for( $i=0;$i<count($plant);$i++){
            if(!empty($plant[$i]['coordinate'])){
                $arr=explode(",",$plant[$i]['coordinate']);
                $AddMarkString .="mark".$plant[$i]['id'].",".$arr[0].",".$arr[1].",".$plant[$i]['name'].",".$plant[$i]['current_state'].",".$plant[$i]['address'].",".$plant[$i][id].",".$plant[$i]['is_active'].",".$plant[$i]['current_state']."+";
            }
        }
        if($AddMarkString){
            echo $AddMarkString;
        }else{
            echo error_get_last();
        }
    }

    //实时查询地图坐标
    public  function map_sel(){
        $hotflag=$_REQUEST['hotflag'];//表示哪一种热点度的站点，0或NULL表示全部，1，2，3，4，5
        if (!isset($hotflag) || empty($hotflag) || !$hotflag){
            $hotflag=0;
        }
        
        echo $hotflag;
        //2---厂商  3---运营商
        if(session('fac_type') == 2){
            if (session('fac_key')){
                $uid = session("fac_key");
                $map[] = array('yy_user_id'=>session('uid'));
            }else{
                $uid = session("fac_login_name");
            }
            $map[] = array('plant_type'=>$uid);
            $table = "ev_fac_bill_total";
        }elseif(session('fac_type') == 3){
            if(session('pid') != 0){
                $map[] = array('yy_user_id'=>session('uid'));
            }else{
                $yyuserid = M()->query("select id from ev_fac where id='".session('uid')."' OR pid='".session('uid')."'");
                foreach($yyuserid as $k=>$v){
                    $yyuserids .= $v['id'].",";
                }
                $yyuserids = trim($yyuserids,',');
                $map['yy_user_id'] = array('IN',$yyuserids);
            }
            $map[] = array('plant_attr'=>3);
            $table = "ev_oper_bill_total";
        }
        $sqll = "SELECT sum(a.total) as total,(sum(a.charge_time) / 3600) as charge_time,sum(a.electricity) as electricity,sum(a.amount) as amount,c.id as ev_plant_id
                    from {$table} a
                    LEFT JOIN ev_char b on b.key_link = a.key_link
                    LEFT JOIN ev_plant c on c.id = b.ev_plant_id
                    WHERE a.state = 1
                    AND a.currentTime >= date_sub(curdate(), interval 3 month)
                    AND a.currentTime < curdate()
                    GROUP BY c.id
                    ORDER BY sum(a.total) DESC";
        $tongji = M()->query($sqll);
        $map['current_state']   = 1;
        $map['is_active']       = 1;
        $plant = M("plant")->field("id,name,address,coordinate,current_state,is_active")->where($map)->select();
        $AddMarkString = '';
        
        $data = array();
        for( $ii=0;$ii<count($plant);$ii++){

            for ($j=0; $j <count($tongji) ; $j++) {

                if ($plant[$ii]['id'] == $tongji[$j]['ev_plant_id']) {

                    $data[] = $tongji[$j];                    
                }       
            }
        }
        //排序
        foreach ($data as $key => $row)
        {
            $volume[$key]  = $row['total'];
        }
        array_multisort($volume, SORT_DESC, $data);

        $percent_count_all=floor(count($data) / 5);
     
        for( $ii=0;$ii<count($plant);$ii++){

            for ($j=0; $j <count($data) ; $j++) {

                if ($plant[$ii]['id'] == $data[$j]['ev_plant_id']) {

                    if($j < $percent_count_all){
                        if ($hotflag!=0 && $hotflag!=5){
                            unset($plant[$ii]);
                            break;
                        }
                        $plant[$ii]['state_i']  = 5;               
                    }elseif ($j >= $percent_count_all && $j < $percent_count_all*2){  
                        if ($hotflag!=0 && $hotflag!=4){
                            unset($plant[$ii]);
                            break;
                        }
                        $plant[$ii]['state_i']  = 4;
                    }elseif ($j >= $percent_count_all*2 && $j < $percent_count_all*3){
                        if ($hotflag!=0 && $hotflag!=3){
                            unset($plant[$ii]);
                            break;
                        }
                        $plant[$ii]['state_i']  = 3;
                    }elseif ($j >= $percent_count_all*3 && $j < $percent_count_all*4){
                        if ($hotflag!=0 && $hotflag!=2){
                            unset($plant[$ii]);
                            break;
                        }
                        $plant[$ii]['state_i']  = 2;
                    }else{ 
                        if ($hotflag!=0 && $hotflag!=1){
                            unset($plant[$ii]);
                            break;
                        }
                        $plant[$ii]['state_i']  = 1;
                    }


                }
            }
        }

        
        
        
        
        
        
        for( $i=0;$i<count($plant);$i++){

            if(!empty($plant[$i]['coordinate'])){
                $arr=explode(",",$plant[$i]['coordinate']);
                $AddMarkString .="mark".$plant[$i]['id'].",".$arr[0].",".$arr[1].",".$plant[$i]['name'].",".$plant[$i]['current_state'].",".$plant[$i]['address'].",".$plant[$i]['id'].",".$plant[$i]['is_active'].",".$plant[$i]['state_i']."+";
            }

        }
        if($AddMarkString){
            echo $AddMarkString;
        }else{
            echo error_get_last();
        }
    }
    //查询当前站点，电桩，状态
    public function map_sel_state_count(){

        $plant_id=$_POST['plant_id'];
        //此站点下所有桩与其对应的状态
        $charInfo = M("char")->where("ev_plant_id='".$plant_id."'")->select();
        if($charInfo){
            foreach($charInfo as $k=>$v){
                $plantInfo = M("plant")->where("id='".$v['ev_plant_id']."'")->find();
                $stateInfo = D("Admin/State")->where("key_link='".$v['key_link']."'")->find();
                $charInfo[$k]['title']       = $plantInfo['name'];
                $charInfo[$k]['address']     = $plantInfo['address'];
                $fac_name = M("fac")->where("fac_login_name='".$v['ev_fac_code']."'")->find();
                if($fac_name['fac_name'] != ''){
                    $charInfo[$k]['trademark']   = $fac_name['fac_name'];
                }else{
                    $charInfo[$k]['trademark']   = "未知";
                }
                $charInfo[$k]['station_key'] = $plantInfo['station_key'];
                $charInfo[$k]['stateInfo']   = $stateInfo;
            }
            echo json_encode($charInfo);
        }
    }

}